package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.exception.UtilException;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.system.domain.vo.SysReimbursementVo;
import com.xjy.system.service.ISysFundaccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.bo.SysPayaccountAddBo;
import com.xjy.system.domain.bo.SysPayaccountQueryBo;
import com.xjy.system.domain.bo.SysPayaccountEditBo;
import com.xjy.system.domain.SysPayaccount;
import com.xjy.system.mapper.SysPayaccountMapper;
import com.xjy.system.domain.vo.SysPayaccountVo;
import com.xjy.system.service.ISysPayaccountService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 支付账户信息Service业务层处理
 *
 * @author zc
 * @date 2022-02-09
 */
@Service
public class SysPayaccountServiceImpl extends ServicePlusImpl<SysPayaccountMapper, SysPayaccount> implements ISysPayaccountService {



    @Override
    public SysPayaccountVo queryById(Long id){
        return getVoById(id, SysPayaccountVo.class);
    }

    @Override
    public TableDataInfo<SysPayaccountVo> queryPageList(SysPayaccountQueryBo bo) {
        PagePlus<SysPayaccount, SysPayaccountVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysPayaccountVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysPayaccountVo> queryList(SysPayaccountQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysPayaccountVo.class);
    }

    private LambdaQueryWrapper<SysPayaccount> buildQueryWrapper(SysPayaccountQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPayaccount> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjId() != null, SysPayaccount::getProjId, bo.getProjId());
        lqw.eq(bo.getAccountId() != null, SysPayaccount::getAccountId, bo.getAccountId());
		lqw.eq(StrUtil.isNotBlank(bo.getAccountType()), SysPayaccount::getAccountType, bo.getAccountType());
		lqw.like(StrUtil.isNotBlank(bo.getAccountName()), SysPayaccount::getAccountName, bo.getAccountName());
        lqw.like(StrUtil.isNotBlank(bo.getAccountBank()), SysPayaccount::getAccountBank, bo.getAccountBank());
        lqw.eq(StrUtil.isNotBlank(bo.getAccountPhone()), SysPayaccount::getAccountPhone, bo.getAccountPhone());
		lqw.in(null != bo.getIdList() && bo.getIdList().size() > 0,SysPayaccount::getId,bo.getIdList());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysPayaccountAddBo bo) {
        SysPayaccount add = BeanUtil.toBean(bo, SysPayaccount.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysPayaccountEditBo bo) {
        SysPayaccount update = BeanUtil.toBean(bo, SysPayaccount.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

	@Override
	public Boolean auditByEditBo(SysPayaccountEditBo bo) {
		SysPayaccountVo sysPayaccountVo = this.queryById(bo.getId());
		if (sysPayaccountVo.getAuditStatus()) {
			throw new UtilException("该数据无需重新审批！");
		}
		bo.setAuditStatus(true);
		return updateByEditBo(bo);
	}

	/**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysPayaccount entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public List<SysPayaccount> queryPageListPay(Long prjId) {
		LambdaQueryWrapper lqw = new LambdaQueryWrapper<SysPayaccount>()
			.eq(SysPayaccount::getProjId,prjId);
    	return list(lqw);
	}
}
